package com.jackson.engineeringtechnologyguide.bean;

/*
 * Author: JacksonCoder
 * FileName: JacksonBean
 * Version: 1.0.0
 * Date: 2025/1/10
 */

import com.jackson.engineeringtechnologyguide.service.IAccountService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 解读 bean 的生命周期
 */

@Slf4j
@Component
public class JacksonBean implements InitializingBean, BeanPostProcessor {

    /**
     * 在依赖注入完成之后就会执行
     */
    @PostConstruct
    private void init() {
        log.info("JacksonBean init...");
    }

    /**
     * 在 BeanFactory 完成属性设置之后执行的， 实现 InitializingBean 接口
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("...");
    }

    /**
     * Bean 初始化前置方法
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        //return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
        if (bean instanceof IAccountService) {
            log.info("...");
        }
        return bean;
    }

    /**
     * Bean 初始化后置方法
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //  return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
        if (bean instanceof IAccountService) {
            log.info("...");
        }
        return bean;
    }

    /**
     * 销毁 bean 的时候回调
     */
    @PreDestroy
    public void destroy() {
        log.info("...");
    }
}
